package com.zjwd.project.service.impl;

import java.util.List;

import com.zjwd.common.constant.UserConstants;
import com.zjwd.common.utils.DateUtils;
import com.zjwd.common.utils.SecurityUtils;
import com.zjwd.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjwd.project.mapper.ProProjectMapper;
import com.zjwd.project.domain.ProProject;
import com.zjwd.project.service.IProProjectService;

/**
 * 建设项目Service业务层处理
 * 
 * @author liquan
 * @date 2024-12-04
 */
@Service
public class ProProjectServiceImpl implements IProProjectService {

    @Autowired
    private ProProjectMapper mapper;

    /**
     * 根据主键ID查询详情
     *
     * @param id 建设项目主键
     * @return 建设项目
     */
    @Override
    public ProProject getModelById(Integer id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询列表
     *
     * @param model 建设项目
     * @return 建设项目
     */
    @Override
    public List<ProProject> selectModelList(ProProject model) {
        return mapper.selectModelList(model);
    }

    /**
     * 校验建设项目编号是否唯一
     *
     * @param proProject 建设项目
     * @return 结果
     */
    @Override
    public boolean checkProjectCodeUnique(ProProject proProject) {
        Integer projectId = StringUtils.isNull(proProject.getProjectId()) ? -1 : proProject.getProjectId();
        ProProject search = mapper.checkProjectCodeUnique(proProject.getProjectCode());
        if (StringUtils.isNotNull(search) && !search.getProjectId().equals(projectId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增
     *
     * @param model 建设项目
     * @return 结果
     */
    @Override
    public int insert(ProProject model) {
        model.setCreateBy(SecurityUtils.getUsername());
        model.setCreateTime(DateUtils.getNowDate());
        model.setPlatformCode(SecurityUtils.getPlatform());
        if (StringUtils.isNotNull(model.getCity()) && model.getCitys().length > 0) {
            model.setProvince(model.getCitys()[0]);
            model.setCity(model.getCitys()[1]);
            model.setCounty(model.getCitys()[2]);
        }
        return mapper.insert(model);
    }

    /**
     * 修改
     *
     * @param model 建设项目
     * @return 结果
     */
    @Override
    public int update(ProProject model) {
        model.setUpdateTime(DateUtils.getNowDate());
        if (model.getCitys().length > 0) {
            model.setProvince(model.getCitys()[0]);
            model.setCity(model.getCitys()[1]);
            model.setCounty(model.getCitys()[2]);
        }
        return mapper.update(model);
    }

    /**
     * 批量删除
     *
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        return mapper.deleteByIds(ids);
    }

    /**
     * 删除
     *
     * @param id 建设项目主键
     * @return 结果
     */
    @Override
    public int delete(Integer id) {
        return mapper.delete(id);
    }
}
